package com.competition.web.admin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

   static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   /**
    * 获取指定时间戳所在月份开始的时间戳/秒
    * @param dateTimeMillis 指定时间戳/毫秒
    * @return
    */
   public static Long getMonthBegin(Long dateTimeMillis) {
       Calendar c = Calendar.getInstance();
       c.setTime(new Date(dateTimeMillis));

       //设置为1号,当前日期既为本月第一天
       c.set(Calendar.DAY_OF_MONTH, 1);
       //将小时至0
       c.set(Calendar.HOUR_OF_DAY, 0);
       //将分钟至0
       c.set(Calendar.MINUTE, 0);
       //将秒至0
       c.set(Calendar.SECOND,0);
       //将毫秒至0
       c.set(Calendar.MILLISECOND, 0);
       // 获取本月第一天的时间戳
       return c.getTimeInMillis() / 1000;
   }
   
   //获取一个月天数
   public static int getDaysOfMonth(Date date) {
   	Calendar calendar = Calendar.getInstance();
   	calendar.setTime(date);
   	return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
   }

   /**
    */
   public static String timeStamp2Date(Long timeLong) {
	    Date date;
	    try {
	        date = sdf.parse(sdf.format(timeLong));
	        return sdf.format(date);
	    } catch (ParseException e) {
	        e.printStackTrace();
	        return "";
	    }
   }
   
   /**
    */
   public static Date timeStampToDate(Long timeLong) {
	    try {
	        return sdf.parse(sdf.format(timeLong));
	    } catch (ParseException e) {
	        e.printStackTrace();
	        return new Date();
	    }
   }
   
   /**
    * 获取指定时间戳所在月份15号的时间戳/秒
    * @param dateTimeMillis 指定时间戳/毫秒
    * @return
    */
   public static Long getMonthMiddle(Long dateTimeMillis) {
       Calendar c = Calendar.getInstance();
       c.setTime(new Date(dateTimeMillis));

       //设置为当月最后一天
       c.set(Calendar.DAY_OF_MONTH, 15);
       //将小时至23
       c.set(Calendar.HOUR_OF_DAY, 23);
       //将分钟至59
       c.set(Calendar.MINUTE, 59);
       //将秒至59
       c.set(Calendar.SECOND,59);
       //将毫秒至999
       c.set(Calendar.MILLISECOND, 999);
       // 获取本月最后一天的时间戳
       return c.getTimeInMillis() / 1000;
   }

   /**
    * 获取指定时间戳所在月份结束的时间戳/秒
    * @param dateTimeMillis 指定时间戳/毫秒
    * @return
    */
   public static Long getMonthEnd(Long dateTimeMillis) {
       Calendar c = Calendar.getInstance();
       c.setTime(new Date(dateTimeMillis));

       //设置为当月最后一天
       c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
       //将小时至23
       c.set(Calendar.HOUR_OF_DAY, 23);
       //将分钟至59
       c.set(Calendar.MINUTE, 59);
       //将秒至59
       c.set(Calendar.SECOND,59);
       //将毫秒至999
       c.set(Calendar.MILLISECOND, 999);
       // 获取本月最后一天的时间戳
       return c.getTimeInMillis() / 1000;
   }

   
   public static Long getWeekStart() {
	   
       Calendar calendar = Calendar.getInstance();
       // 一周第一天为周日，所以此处日+1
       calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR), 2);
       calendar.set(calendar.get(Calendar.YEAR),
               calendar.get(Calendar.MONTH),
               calendar.get(Calendar.DAY_OF_MONTH),
               0, 0, 0);
      return calendar.getTimeInMillis();
   }
   
   public static Long getWeeEnd() {
	   // 一周第一天为周日，所以此处为下一周第一天
	   Calendar calendar = Calendar.getInstance();
       calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR)+1, 1);
       calendar.set(calendar.get(Calendar.YEAR),
               calendar.get(Calendar.MONTH),
               calendar.get(Calendar.DAY_OF_MONTH),
               23, 59, 59);
       
       return calendar.getTimeInMillis();
   }
   
   /**
    * 获取上一个月1号0点0分0秒的时间戳
    * @param dateTimeMillis
    * @return
    */
   public static Long getPreviousMonthBegin(Long dateTimeMillis){
       Date date = new Date(dateTimeMillis);
       date.setDate(1);
       date.setHours(0);
       date.setMinutes(0);
       date.setSeconds(0);
       if(date.getMonth() - 1 <= 0){
           date.setYear(date.getYear() - 1);
           date.setMonth(12);
       }else {
           date.setMonth(date.getMonth() - 1);
       }
       return date.getTime() / 1000;
   }

   /**
    * 获取当前日期
    * @return
    */
   public static String getCurrentDate() {
       Date date = new Date();
       return sdf.format(date);
   }
}